﻿//查询列表页面
function viewModel(data) {
    var self = this;
    this.dataSource = data.dataSource;
    this.form = ko.mapping.fromJS(data.form);
    this.grid = {
        size: { w: 4, h: 94 },
        url: '/api/pacm/mbjh',
        queryParams: ko.observable(),
        pagination: true,
        customLoad: false,
        idField: 'Zlmbjh_BillNo'
    };
    this.gridEdit = new com.editTreeGridViewModel(this.grid);
    this.grid.onDblClickRow = self.gridEdit.begin;
    this.grid.onClickRow = self.gridEdit.ended;
    this.grid.OnAfterCreateEditor = function (edt) {
        com.readOnlyHandler('input')(edt["Zlmbjh_BillNo"].target, true);
    };
    //刷新
    this.refreshClick = function () {
        window.location.reload();
    };
    //增加
    this.addClick = function () {
        com.ajax({
            type: 'GET',
            url: "/api/pacm/mbjh/GetNewBillNo",
            success: function (d) {
                var syear = data.dataSource.curYear;
                var smonth = data.dataSource.curMonth;
                var row = { Zlmbjh_BillNo: d, Zlmbjh_Year: syear, Zlmbjh_Month: smonth,Zlmbjh_A199D:"1",Zlmbjh_A199X:1 };
                self.gridEdit.addnew(row);
            }
        });
    }
    //编辑
    this.editClick = function () {
        var row = self.grid.treegrid('getSelected');
        //        if (row["ApproveState"] == undefined) {
        //            return;
        //        }
        self.gridEdit.begin(row);
    }
    //双击事件
    //this.grid.onDblClickRow = this.editClick;
    //审核
    this.auditClick = function () {
        var row = self.grid.treegrid('getSelected');
        if (!row) return com.message('warning', "请选择一条明细数据!");
        com.auditDialog(function (d) {
            com.ajax({
                type: 'POST',
                url: "/api/pacm/mbjh/audit/" + row["Zlmbjh_BillNo"],
                data: JSON.stringify(d),
                success: function () {
                    com.message('success', "单据已通过审核");
                }
            });
        });
    };
    //删除
    this.deleteClick = function () {
        var row = self.grid.treegrid('getSelected');
        if (!row) return com.message('warning', "请选择待删除的单据!");
        com.message('confirm', "确定要删除选中的单据吗", function (b) {
            if (b) {
                com.ajax({
                    type: 'DELETE',
                    url: "/api/pacm/mbjh/" + row["Zlmbjh_BillNo"],
                    success: function () {
                        com.message('success', "删除成功");
                        //self.searchClick();
                        self.refreshClick();
                    }
                });
            }
        });
    }
    //保存
    this.saveClick = function () {
        self.gridEdit.ended();
        var post = {};
        post.list = self.gridEdit.getChanges(['Zlmbjh_BillNo', 'Zlmbjh_Year', 'Zlmbjh_Month','Zlmbjh_Dwid',
          'Zlmbjh_A199D', 'Zlmbjh_A199X', 'Zlmbjh_Jyhgl', 'Zlmbjh_Cchgl', 'Zlmbjh_Zzhgl', 'Zlmbjh_Pbhgl', 'Zlmbjh_Remark']);
        if (self.gridEdit.isChangedAndValid) {
            com.ajax({
                url: '/api/pacm/mbjh/edit',
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', '保存成功！');
                    //self.grid.queryParams({ CodeType: self.CodeType() });
                    self.gridEdit.accept();
                }
            });
        }
    };
    this.searchClick = function () {
        var param = ko.toJS(this.form);
        this.grid.queryParams(param);
    }
    //    this.clearClick = function () {
    //        $.each(self.form, function () { this(''); });
    //        this.searchClick();
    //    }
}